<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
	<title></title>
</head>
<body>
   <h3>YouCube</h3>
   <input type="button" name="" id="search" value="search" onclick="searchBlog()">
   <input type="text" name="searchtext" id="searchtext">
   <div id="blog"></div>
   <input type="button" id="showall" value="显示输入的内容" onclick="showBlog();">
</body>
<script type="text/javascript">
		function Blog(body,date) {
			this.body = body;
			this.date = date;
		}
		var blog = [new Blog("Got",new Date("08/14/2016")),new Blog ("Solved",new Date("08/19/2008")),new Blog ("Managed",new Date("08/16/2008")),new Blog ("Found",new Date("08/21/2008"))];
// 传入数据
		function showBlog(numEntries) {
			// 排序
			blog.sort(function(blog1,blog2){return blog2.date - blog1.date;});
			if( !numEntries )
				numEntries = blog.length;
			var i=0,blogText="";
			while(i<blog.length && i<numEntries) {
				if( i % 2 ==0) //偶数行的北京颜色改变
					blogText += "<p style='background-color: #eeeeee'>";
				else
					blogText += "<p>";
				blogText += "<strong>" + (blog[i].date.getMonth()+1) + "/" + blog[i].date.getDate() + "/" + blog[i].date.getFullYear() + "</strong><br/>" + blog[i].body + "</p>";
				i++;
			}
			document.getElementById('blog').innerHTML = blogText;
		}		
//  搜索函数
        function searchBlog() {
        	var searchText = document.getElementById('searchtext').value;
        	for(var i=0;i<blog.length;i++) {
        		if(blog[i].body.toLowerCase().indexOf(searchText.toLowerCase()) != -1) {
        			alert("[" + (blog[i].date.getMonth() + 1 ) + "/" + blog[i].date.getDate() + "/" + blog[i].date.getFullYear() + "]" + blog[i].body );
        			break;
        		}
        	}
        	if(i == blog.length)
        		alert("Sorry,there are no blog enteries containing the search text.");
        }
// 随机函数
        function randomBlog() {
        	var i = Math.floor(Math.random() * blog.length);
        	alert("[" + (blog[i].date.getMonth() + 1 ) + "/" + blog[i].date.getDate() + "/" + blog[i].date.getFullYear() + "]" + blog[i].body );
        }
</script>
</html>